package com.sunday.ble.orlant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sunday.ble.orlant.BaseApp;
import com.sunday.ble.orlant.R;
import com.sunday.ble.orlant.adapter.HistoryAdapter;
import com.sunday.ble.orlant.event.DoOption;
import com.sunday.ble.orlant.greendao.Device;
import com.sunday.ble.orlant.greendao.DeviceDao;
import com.sunday.member.base.BaseActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HistoryActivity extends BaseActivity {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.title_view)
    TextView titleView;

    private List<Device> deviceList;
    private DeviceDao deviceDao;
    private HistoryAdapter adapter;
    private LinearLayoutManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        titleView.setText("我的设备");
        deviceDao = BaseApp.getInstance().getDaoSession().getDeviceDao();
        deviceList = deviceDao.loadAll();
        adapter = new HistoryAdapter(mContext, deviceList);
        manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL));
        adapter.setOnItemListener(new HistoryAdapter.OnItemListener() {
            @Override
            public void itemClick(int position, final Device device) {
                intent = new Intent(mContext, UpdateNameActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("device", device);
                intent.putExtra("device", bundle);
                startActivity(intent);
            }
        });
    }

    public void onEvent(DoOption option) {
        if (option.updateName) {
            deviceList = deviceDao.loadAll();
            adapter.refresh(deviceList);
            //adapter.notifyDataSetChanged();
        }
    }
}
